package com.example.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.example.entity.SysUser;
import com.example.service.ISysLoginService;
import com.example.utils.R;
import com.example.utils.ShiroUtils;
import com.google.code.kaptcha.servlet.KaptchaExtend;

/**
 * 登录相关
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年11月10日 下午1:15:31
 */
@Controller
public class SysLoginController extends KaptchaExtend {
	
	@Autowired
	private ISysLoginService sysLoginService;
	
	@RequestMapping("test")
	@ResponseBody
	public R test() {
		Page<SysUser> page = new Page<SysUser>();
		page.setCurrent(1);
		page.setSize(10);
		//List<SysUser> sysUsers = sysLoginService.getSysUsersPage(page);
		List<SysUser> sysUsers = sysLoginService.getSysUsers();
		R r = new R();
		r.put("sysUsers", sysUsers);
		return r;
	}
	
	@RequestMapping("captcha.jpg")
	public void captcha(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.captcha(req, resp);
	}
	
	/**
	 * 登录
	 */
	@ResponseBody
	@RequestMapping(value = "/sys/login", method = RequestMethod.GET)
	public R login(HttpServletRequest request, String username, String password, String captcha)throws IOException {
		/*if(!captcha.equals(getGeneratedKey(request))){
			return R.error("验证码不正确");
		}*/
		username = "admin";
		password = "admin1";
		try{
			Subject subject = ShiroUtils.getSubject();
			//sha256加密
			//password = new Sha256Hash(password).toHex();
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			subject.login(token);
		}catch (UnknownAccountException e) {
			return R.error("账号或密码错误");
		}catch (IncorrectCredentialsException e) {
			return R.error("账号或密码错误");
		}catch (LockedAccountException e) {
			return R.error("账号已锁定");
		}catch(ExcessiveAttemptsException e){ 
			return R.error("登录失败次数过多");
        }catch(DisabledAccountException e){
        	return R.error("账号已禁用");
        }catch(ExpiredCredentialsException e){
        	return R.error("过期的凭证");
        }catch (AuthenticationException e) {
			return R.error("账户验证失败");
		}
	    
		return R.ok();
	}
	
	/**
	 * 退出
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout() {
		ShiroUtils.logout();
		return "redirect:login.html";
	}
	
}
